// pages/components/selectBuild/selectBuild.js
//引入请求接口文件
import Serv from './buildingServ';
var regeneratorRuntime = require("../../lib/js/runtime-module.js")
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    id: { //优惠券样式类型
      type: String,
      value: ''
    },

  },

  /**
   * 组件的初始数据
   */
  data: {
    nearbyBuild: [],
    placeList: [],
    // 控制弹框显示
    showBuildList: false,
    // 控制附近还是更多的显示
    nearby: true,
    //暂存的数据
    provinceBasket: [],
    cityBasket: [],
    houseBasket: [],
    //tab当前选项高亮
    selectCur: 1,
    activeIndex: 0,
  },
  ready() {
    this.getLocal()
    this.getAreaAddress()
  },
  /**
   * 组件的方法列表
   */
  methods: {

    //获取区域地址
    async getAreaAddress() {
      let result = await Serv.areAddressGet({})
      if (result.success && result.data) {
        let data = result.data
        let arr = [data[0]]
        let attr = [data[data.length - 1]]
        data[0] = {
          label: "北京",
          value: "110100",
          children: arr
        }
        data[data.length - 1] = {
          label: "澳门",
          value: "820000",
          children: attr
        }
       
        this.setData({
          provinceBasket: data || []
        });
      }
      // console.log(result,"address")
    },
    //显示/隐藏弹窗
    showModal: function (e) {
      if(!app.globalData.cityData){
        this.setData({ showBuildList: true, nearby: false,placeList: this.data.provinceBasket, selectCur: 1})
      }else{
        this.setData({ showBuildList: true, nearby: true })
      }
    },
    hideModal: function () {
      this.setData({ showBuildList: false })
      let { myEventDetail }= this.data
      this.triggerEvent('myevent', { show: true, ...myEventDetail})
    },
    hideModalforBuild: function () {
      this.setData({ showBuildList: false })
    },
    showMoreBuild() {
      this.setData({ nearby: false, placeList: this.data.provinceBasket, selectCur: 1 })
    },
    // 选择附近楼盘
    selectBuild(e) {
      // 设置要传回page的值
      console.log(e.currentTarget.dataset, "dataset")
      let { value, label, code, index, effect, smallimg} = e.currentTarget.dataset
      var myEventDetail = { value, label, code, index, effect, smallimg } // detail对象，提供给事件监听函数
      var myEventOption = {} // 触发事件的选项
      console.log(myEventDetail)
      this.setData({
        myEventDetail
      })
      this.triggerEvent('myevent', myEventDetail, myEventOption)
      // 设置被选中的高亮
      this.setData({ activeIndex: index })
      this.hideModalforBuild()
    },
    // 获取当前选中项
    getCurrentItem() {
      let { activeIndex, nearby, nearbyBuild, placeList } = this.data
      let list = nearby ? nearbyBuild : placeList
      return list[activeIndex] || {}
    },

    init() {
      let { id, activeIndex, nearby, nearbyBuild = [] } = this.data
      if (nearby && id) {
        nearbyBuild.map((item, index) => {
          if (id == item.value) {
            activeIndex = index
          }
        })
        this.setData({ activeIndex })
      }
    },
    //获取房源
    async getHouseResource(cityCode, key) {
      console.log('.................tttttttttttttttttt')
      let result = await Serv.houseByCity(cityCode)
      console.log(result);
      if (result.success && result.rows) {
        let rows = result.rows.map(item => ({
          value: item.productId,
          label: item.resultName,
          code: item.projectCode,
          productId: item.productId,
          resultName: item.resultName,
          projectCode: item.projectCode,
          effect: item.effect,
          smallimg: item.smallimg
        }))
        this.setData({
          [key == 'nearbyBuild' ? "nearbyBuild" : "placeList"]: rows,
          houseBasket: key != 'nearbyBuild' ? rows : []
        })
        // console.log(this.data.placeList,'this.data.placeList')
      } else {
        this.setData({
          [key == 'nearbyBuild' ? "nearbyBuild" : "placeList"]: []
        })
      }
      this.init()
    },
    //切换tab
    changeTab(e) {
      let index = e.currentTarget.dataset.index;
      let { provinceBasket, cityBasket, houseBasket } = this.data
      let tabData = { 1: provinceBasket, 2: cityBasket, 3: houseBasket }
      this.setData({
        placeList: tabData[index],
        selectCur: index / 1
      })
    },
    //选择更多
    selectMore(e) {
      let areaidx = e.currentTarget.dataset.areaidx
      let targetValue = e.currentTarget.dataset.value
      let index = this.data.selectCur
      let { provinceBasket } = this.data;
      let city = provinceBasket[areaidx].children

      //每次点击进入下一步,若执行到第3步,则输出所选值
      if (index == 3) {
        this.setData({ selectCur: 1, activeIndex: null })
        this.hideModal()
        // 设置要传回page的值
        let { value, label, code, effect, smallimg} = e.currentTarget.dataset
        var myEventDetail = { value, label, code, index, effect, smallimg } // detail对象，提供给事件监听函数
        var myEventOption = {} // 触发事件的选项
        this.triggerEvent('myevent', myEventDetail, myEventOption)
        console.log(myEventDetail)
        this.setData({ activeIndex: areaidx })
      } else {
        if (index == 1) {
          this.setData({ placeList: city, cityBasket: city })
        } else {
          this.getHouseResource(targetValue)
        }
        this.setData({ selectCur: index + 1 })
      }
      this.setData({top:0})
      console.log(this.data.top)
    },

    // 获取当前地理位置
    getLocal: function (latitude, longitude) {

     /* const that = this;
      wx.getLocation({
        type: 'wgs84',
        success: async function (res) {
          let { data, success } = await Serv.locationGet({ lat: res.latitude, lng: res.longitude })
          if (success) {
            that.getHouseResource(data.code || 110100, "nearbyBuild")
          }
          console.log(data, "data")
        }
      })*/
      console.log(app.globalData.cityData)
      console.log('.................tttttttttttttttttt')
      if(app.globalData.cityData){
        this.getHouseResource(Number(app.globalData.cityData.cityadcode) || 110100, "nearbyBuild")
      }
    }
  }
})
